use fltk::{
    text::{TextEditor, TextBuffer},
    enums::{Color, FrameType, Event},
    button::Button,
    prelude::*,
};
use std::cell::RefCell;
use std::rc::Rc;
use crate::common::event::AppEvent;

// 常量定义
const PADDING: i32 = 5;  // 减小间距
const BTN_WIDTH: i32 = 100;  // 增加按钮宽度

// 颜色常量
const COLOR_SYSTEM_BG: Color = Color::from_rgb(240, 240, 240);  // 系统界面背景色
const COLOR_WHITE: Color = Color::White;  // 纯白色
const COLOR_TEXT: Color = Color::from_rgb(0, 0, 0);  // 文本颜色
const COLOR_APP_BG: Color = Color::from_rgb(238, 238, 238);  // 应用背景色

pub struct BottomPanel {
    _app_width: i32,    // app width
    _app_height: i32,   // app height
    _start_x: i32,      // position x of the panel
    _start_y: i32,      // position y of the panel
    _width: i32,        // width of the panel
    _height: i32,       // height of the panel
    _editor: TextEditor,
    _btn_send: Button,
    _buffer: Rc<RefCell<TextBuffer>>,  // 使用Rc<RefCell>包装buffer以便在回调中使用
}

impl BottomPanel {
    pub fn new(app_width: i32, app_height: i32, start_x: i32, start_y: i32) -> Self {
        let (w, h) = Self::draw_area_size(app_width, app_height, start_x, start_y);

        // 创建文本编辑器（宽度需要减去按钮宽度和间距）
        let editor_width = w - BTN_WIDTH - PADDING;
        let buffer = Rc::new(RefCell::new(TextBuffer::default()));
        let mut text_editor = TextEditor::new(start_x, start_y, editor_width, h, "");

        text_editor.set_buffer(buffer.borrow().clone());
        text_editor.set_frame(FrameType::DownBox);
        text_editor.set_color(COLOR_WHITE);  // 设置为纯白色
        text_editor.set_text_color(COLOR_TEXT);  // 文本颜色为黑色
        text_editor.set_selection_color(COLOR_APP_BG);  // 选中文本的背景色
        text_editor.set_text_size(12);
        text_editor.redraw();

        // 创建发送按钮 - 与文本编辑器等高
        let btn_x = start_x + editor_width + PADDING;
        let mut btn_send = Button::new(btn_x, start_y, BTN_WIDTH, h, "发送");
        btn_send.set_color(COLOR_SYSTEM_BG);  // 使用系统背景色
        btn_send.set_label_color(COLOR_TEXT);  // 文字为黑色
        btn_send.set_frame(FrameType::UpBox);  // 使用凸起效果
        btn_send.set_label_size(12);
        btn_send.set_selection_color(COLOR_APP_BG);  // 点击时变为应用背景色

        // 设置按钮事件处理
        btn_send.handle(move |b, ev| {
            match ev {
                Event::Push => {
                    println!("发送按钮: 按下");
                    true
                }
                Event::Released => {
                    println!("发送按钮: 弹起");
                    true
                }
                _ => false,
            }
        });

        Self {
            _app_width:     app_width,
            _app_height:    app_height,
            _start_x:       start_x,
            _start_y:       start_y,
            _width:         w,
            _height:        h,
            _editor:        text_editor,
            _btn_send:      btn_send,
            _buffer:        buffer,
        }
    }

    // 获取当前输入的文本
    pub fn get_text(&self) -> String {
        self._buffer.borrow().text()
    }

    // 清空输入框
    pub fn clear_text(&mut self) {
        self._buffer.borrow_mut().set_text("");
    }

    fn draw_area_size(app_width: i32, app_height: i32, sx: i32, sy: i32) -> (i32, i32) {
        let w = app_width - sx;
        let h = app_height - sy;  // the same as center panel
        (w, h)
    }

    pub fn get_coordinates(&self) -> (i32, i32, i32, i32) {
        (self._start_x, self._start_y, self._start_x + self._width, self._start_y + self._height)
    }

    #[allow(dead_code)]
    pub fn handle_event(&mut self, event: &AppEvent) {
        println!("BottomPanel handleEvent {:?}", event);
    }
}
